using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace App
{

    public class BtnLb : Label
    {
        private bool isflat;
        private bool ispressed, hover, pressed;
        private Timer m_timer = new Timer();
        //Fading
        bool m_isFad = true;
        int m_fad = 0; //0 nothing, 1 entering, 2 leaving
        int m_value = 255; //Level of transparency

        public bool Isflat
        {
            get { return isflat; }
            set { isflat = value; Invalidate(); }
        }

        public bool Ispressed
        {
            get { return ispressed; }
            set { ispressed = value; Invalidate(); }
        }

        public BtnLb()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.ForeColor = Color.FromArgb(21, 66, 139);
            this.Margin = new Padding(1);
            this.Padding = new Padding(2);
            this.TextAlign = ContentAlignment.MiddleCenter;
            this.isflat = false;
            this.ispressed = false;
            this.BackColor = Color.Transparent;
            m_timer.Interval = 10;
            m_timer.Tick += new EventHandler(m_timer_Tick);
        }


        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            this.AutoSize = false;
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.pressed = true;
            this.Invalidate();
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            this.pressed = false;
            this.Invalidate();
        }
        protected override void OnMouseEnter(EventArgs e)
        {
            if (m_isFad)
            {
                m_fad = 1;
                m_timer.Start();
            }
            base.OnMouseEnter(e);
            this.hover = true;
            this.Invalidate();
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            if (m_isFad)
            {
                m_fad = 2;
                m_timer.Start();
            }
            base.OnMouseLeave(e);
            if (m_value == 0)
            {
                this.hover = false;
            }
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;

            Rectangle rect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);

            if (this.hover)
                DrawHelper.RenderSelection(e.Graphics, rect, 2f, this.pressed);
            else if (this.ispressed)
                DrawHelper.RenderSelection(e.Graphics, rect, 2f, this.pressed);
            else if (!this.isflat)
            {
                switch (Common.Theme)
                {
                    case Common.ColorScheme.Blue:
                        {
                            Color[] col = new Color[] { Color.FromArgb(225, 234, 245), Color.FromArgb(209, 223, 240), 
                                Color.FromArgb(199, 216, 237), Color.FromArgb(231, 242, 255) };
                            float[] pos = new float[] { 0.0f, 0.2f, 0.2f, 1.0f };

                            LinearGradientBrush brush = DrawHelper.GetBrush(col, pos, rect);
                            DrawHelper.FillRoundRectangle(e.Graphics, brush, rect, 2f);
                            DrawHelper.DrawRoundRectangle(e.Graphics, new Pen(Color.FromArgb(141, 178, 227)), rect, 2f);
                            rect.Offset(1, 1);
                            rect.Width -= 2;
                            rect.Height -= 2;
                            DrawHelper.DrawRoundRectangle(e.Graphics, new Pen(new LinearGradientBrush(rect, 
                                Color.FromArgb(231, 239, 248), Color.Transparent, LinearGradientMode.ForwardDiagonal)), rect, 2f);
                            brush.Dispose();
                        }
                        break;
                    case Common.ColorScheme.Custom:
                        {
                            Color[] col = new Color[] { DrawHelper.GetColor(1.025, Common.Color), 
                                DrawHelper.GetColor(1.0,Common.Color), DrawHelper.GetColor(0.975,Common.Color), 
                                DrawHelper.GetColor(1.075,Common.Color) };
                            float[] pos = new float[] { 0.0f, 0.2f, 0.2f, 1.0f };

                            LinearGradientBrush brush = DrawHelper.GetBrush(col, pos, rect);
                            DrawHelper.FillRoundRectangle(e.Graphics, brush, rect, 2f);
                            DrawHelper.DrawRoundRectangle(e.Graphics, new Pen(DrawHelper.GetColor(0.75, Common.Color)), rect, 2f);
                            rect.Offset(1, 1);
                            rect.Width -= 2;
                            rect.Height -= 2;
                            DrawHelper.DrawRoundRectangle(e.Graphics, new Pen(new LinearGradientBrush(rect,
                                DrawHelper.GetColor(1.05, Common.Color), Color.Transparent, LinearGradientMode.ForwardDiagonal)), rect, 2f);
                            brush.Dispose();
                        }
                        break;
                }
            }
            if(m_fad != 0)
            {
                Brush br = new SolidBrush(Color.FromArgb(m_value, 255, 255, 255));
                DrawHelper.FillRoundRectangle(e.Graphics, br, rect, 2f);
            }

            base.OnPaint(e);
        }


        void m_timer_Tick(object sender, EventArgs e)
        {
            switch (m_fad)
            {
                case 1:
                    {
                        if (m_value == 0)
                        {
                            m_value = 255;
                        }
                        if (m_value > 15)
                        {
                            this.Invalidate();
                            m_value -= 10;
                        }
                        else
                        {
                            m_value = 0;
                            this.Invalidate();
                            m_timer.Stop();
                        }
                        break;
                    }
                case 2:
                    {
                        if (m_value == 0)
                        {
                            m_value = 255;
                        }
                        if (m_value > 15)
                        {
                            this.Invalidate();
                            m_value -= 10;
                        }
                        else
                        {
                            m_value = 0;
                            this.Invalidate();
                            m_timer.Stop();
                            hover = false;
                            m_fad = 0;
                        }
                        break;
                    }
            }
        }
    }
}